package com.slz3.business.module.wmsManagement.wmsStockDetail.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.wmsManagement.wmsStockDetail.domain.WmsStockDetail;
import com.slz3.business.module.wmsManagement.wmsStockDetail.domain.WmsStockDetailFactory;
import com.slz3.business.module.wmsManagement.wmsStockDetail.steward.WmsStockDetailSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class WmsStockDetailServiceImpl implements WmsStockDetailService {
    @Override
    public WmsStockDetail save(WmsStockDetail wmsStockDetail) {
        return Repositorys.wmsStockDetail.save(WmsStockDetailFactory.initBaseFields(wmsStockDetail));
    }

    @Override
    public Page<WmsStockDetail> page(MultiValueMap<String, String> params) {
        Page<WmsStockDetail> wmsStockDetailPage = Repositorys.wmsStockDetail.findAll(
                WmsStockDetailSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return wmsStockDetailPage;
    }


    @Override
    public WmsStockDetail modify(WmsStockDetail wmsStockDetailNew) {
        return Repositorys.wmsStockDetail.saveAndFlush(WmsStockDetailFactory.initBaseFields(wmsStockDetailNew));
    }

    @Override
    public Optional<WmsStockDetail> single(UUID uuid) {
        return Repositorys.wmsStockDetail.findById(uuid);
    }

    @Override
    public List<WmsStockDetail> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsStockDetail.findAllById(uuids);
    }

    @Override
    public List<WmsStockDetail> delete(List<WmsStockDetail> wmsStockDetailList) {
        wmsStockDetailList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsStockDetailFactory.initBaseFields(item);
        });
        return Services.wmsStockDetailTransactional.batchModify(wmsStockDetailList);
    }

}
